package com.atguigu.mall.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.mall.product.dto.CategoryBrandRelationDTO;
import com.atguigu.mall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.mall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 品牌分类关联
 *
 * @author Mr·L
 * @email 907298149@qq.com
 * @date 2021-12-02 20:39:47
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(CategoryBrandRelationEntity params){
        PageUtils page = categoryBrandRelationService.queryPage(params);
        return R.ok().put("page", page);
    }

    @RequestMapping("/brands/list")
    public R brandsList(Long catId){
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.brandsList(catId);
        return R.ok().put("data", data);
    }





    @PostMapping("/save/relation")
    public R saveRelation(@RequestBody CategoryBrandRelationDTO dto){
        categoryBrandRelationService.saveRelation(dto);
        return R.ok();
    }


    @RequestMapping("/queryListByBrandId")
    public R queryListByCategoryId(Long brandId){
        List<CategoryBrandRelationEntity> list = categoryBrandRelationService.queryListByBrandId(brandId);
        List<Long> categoryIdList = list.stream().map(CategoryBrandRelationEntity::getCatelogId).distinct().collect(Collectors.toList());
        return R.ok().put("categoryIdList", categoryIdList);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);
        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.save(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
